<?php

/*
 * Copyright (C) 2014 University of Pittsburgh
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once('StaticFunctions.inc');
require_once('Constants.inc');

class HTMLSelect {
  
  public $id = '';
  public $name = '';
  public $is_multiple = false;
  public $on_change = "";
  public $size = 0;
  public $style = "";
  public $is_dijit = false;
  public $key_val_map = array();
  
  public function __construct() {
  
  }
  
  public function get_html($indent = 0) {
    $html = "\n";
    $html .= (StaticFunctions::indent($indent, Constants::HTML_INDENT_SIZE) . "<select id=\"" . $this->id . "\"" .
               ($this->name === '' ? " name=\"" . $this->id . "\""  : ($this->is_multiple ? " name=\"" . $this->name . "[]\"" : " name=\"" . $this->name . "\"")) .
               ($this->is_multiple && $this->is_dijit ? " data-dojo-type=\"dijit/form/MultiSelect\" multiple=\"true\"" : 
                 ($this->is_multiple ? " multiple=\"multiple\"" : 
                   ($this->is_dijit ? " data-dojo-type=\"dijit/form/Select\"" : ""))) . 
               ($this->on_change === '' ? "" : " onchange=\"" . $this->on_change . "\"") .
               ($this->style === '' ? "" : " style=\"" . $this->style . "\"") .
               ($this->size == 0 ? "" : " size=\"" . $this->size . "\"") . ">\n");
    $first_row = true;
    foreach($this->key_val_map as $key=>$value) {
      if($first_row) {
        $html .= (StaticFunctions::indent($indent + 1, Constants::HTML_INDENT_SIZE) . "<option value = \"" . $key . "\" selected=\"selected\">" . $value . "</option>\n");
        $first_row = false;
      } else {
        $html .= (StaticFunctions::indent($indent + 1, Constants::HTML_INDENT_SIZE) . "<option value = \"" . $key . "\">" . $value . "</option>\n");
      }
    }
    $html .= (StaticFunctions::indent($indent,  Constants::HTML_INDENT_SIZE) . "</select>\n");
    return $html;
  }
  
}

?>